package com.sifeng.test;

import com.sifeng.pojo.WorkloadRatioDTO;
import com.sifeng.utils.MyUtils;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang_gsh on 2018/10/30.
 * 查询需求功能点数及比例
 */
public class WriteRequirementFunctionPoint {


	public static void writeExcel(String path, List<WorkloadRatioDTO> workloadRatioDTOList) throws IOException {
		XSSFWorkbook wb = MyUtils.getXSSFWorkbook(path);
		XSSFSheet sheet = wb.getSheetAt(0);
		if (sheet == null) {
			return;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		for (WorkloadRatioDTO workloadRatioDTO : workloadRatioDTOList) {
			String pointRatio = workloadRatioDTO.getPointRatio();
			String[] pointRatioRec = pointRatio.split("；");
			map.put(workloadRatioDTO.getRequirementNumber(), pointRatioRec);
		}

		XSSFCellStyle cellStyle = wb.createCellStyle();
		cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));

		for (int i = 1; i <= sheet.getLastRowNum(); i++)  {
			XSSFRow row = sheet.getRow(i);
			if(row == null) {
				continue;
			}
			XSSFCell cellC = row.getCell(2);
			if(cellC == null) {
				continue;
			}
			cellC.setCellType(Cell.CELL_TYPE_STRING);
			String requirementNumber = cellC.getStringCellValue();
			if(map.containsKey(requirementNumber)) {
				String[] pointRatioRec = (String[])map.get(requirementNumber);
				for (String s : pointRatioRec) {
					String[] sRec = s.split("：");
					int writeIndex = 0;
					switch (sRec[0].trim()) {
						case "银行合作研发部" :
							writeIndex = 5;
							break;
						case "移动支付研发部" :
							writeIndex = 6;
							break;
						case "营销服务研发部" :
							writeIndex = 7;
							break;
						case "电商支付研发部" :
							writeIndex = 8;
							break;
						case "风控大数据研发部" :
							writeIndex = 9;
							break;
						case "互联网金融研发部" :
							writeIndex = 10;
							break;
						case "前端技术研发部" :
							writeIndex = 11;
							break;
						case "基础应用研发部" :
							writeIndex = 12;
							break;
						case "客户端研发部" :
							writeIndex = 13;
							break;
						case "公共缴费研发部" :
							writeIndex = 14;
							break;
						default:
							break;
					}

					if(writeIndex > 0) {
						XSSFCell cellF = row.getCell(writeIndex);
						cellF.setCellValue(sRec[1]);
						cellF.setCellStyle(cellStyle);
					}
				}
			} else {
				continue;
			}
		}

		FileOutputStream outputStream = new FileOutputStream(path);

		wb.write(outputStream);
		outputStream.flush();
		wb.close();
	}

	public static void main(String[] args) throws IOException, InvalidFormatException, ParseException {
		String path = "E:\\hisun\\开发三部\\功能点评估表\\201904\\";
		String writePath = "E:\\hisun\\开发三部\\功能点评估表\\201904\\2019年4月份开发工作占比明细.xlsx";
		List<WorkloadRatioDTO> workloadRatioDTOList = QueryRequirementFunctionPoint.execute(path);
		WriteRequirementFunctionPoint.writeExcel(writePath,workloadRatioDTOList);
	}
}
